/**
 * 
 */
package com.james.wee.shuangseqiu.killnum;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

import com.james.wee.shuangseqiu.bo.IDBBo;
import com.james.wee.shuangseqiu.bo.OpenNumber;
import com.james.wee.shuangseqiu.db.DB;
import com.james.wee.shuangseqiu.tools.ToolsUtil;

/**
 * @author JamesWee
 * 
 */
public class KillNumberDb {
	private OpenNumber openNum;
	private Map<String, String> map_01 = null;
	private Map<String, String> map_02 = null;
	private Map<String, String> map_03 = null;
	private Map<String, String> map_04 = null;
	private Map<String, String> map_05 = null;
	private Map<String, String> map_06 = null;
	private Map<String, String> mapXing = null;

	public KillNumberDb() {
		String[] _01 = "01,07,13,19,25,31".split(",");
		map_01 = new HashMap<String, String>();
		for (String s01 : _01) {
			map_01.put(s01, s01);
		}
		String[] _02 = "02,08,14,20,26,32".split(",");
		map_02 = new HashMap<String, String>();
		for (String s02 : _02) {
			map_02.put(s02, s02);
		}
		String[] _03 = "03,09,15,21,27,33".split(",");
		map_03 = new HashMap<String, String>();
		for (String s03 : _03) {
			map_03.put(s03, s03);
		}
		String[] _04 = "04,10,16,22,28".split(",");
		map_04 = new HashMap<String, String>();
		for (String s04 : _04) {
			map_04.put(s04, s04);
		}
		String[] _05 = "05,11,17,23,29".split(",");
		map_05 = new HashMap<String, String>();
		for (String s05 : _05) {
			map_05.put(s05, s05);
		}
		String[] _06 = "06,12,18,24,30".split(",");
		map_06 = new HashMap<String, String>();
		for (String s06 : _06) {
			map_06.put(s06, s06);
		}

		mapXing = new HashMap<String, String>();
		mapXing.put("01", "01,10,19,28");
		mapXing.put("02", "02,11,20,29");
		mapXing.put("10", "03,12,21,30");
		mapXing.put("11", "04,13,22,31");
		mapXing.put("12", "05,14,23,32");
		mapXing.put("20", "06,15,24,33");
		mapXing.put("21", "07,16,25");
		mapXing.put("22", "08,17,26");
		mapXing.put("00", "09,18,27");

	}

	public OpenNumber getOpenNum() {
		return openNum;
	}

	public void setOpenNum(OpenNumber openNum) {
		this.openNum = openNum;
	}

	private long muitly(String opennums) {
		String[] str = opennums.split(",");
		long r = 1;
		for (String s : str) {
			r = r * Integer.parseInt(s.trim());
		}
		return r;
	}

	/**
	 * 根据开奖号的相乘积再拆分求和相减
	 * @return
	 */
	public String muiltyHeZhi() {
		long mu = 0;
		mu = muitly(openNum.sixRedNum());
		int index = 0;
		for (; mu > 0;) {
			index += (mu % 10);
			mu = mu / 10;
		}
		String[] str = openNum.sixRedNum().split(",");
		Map<String, String> retmp = getMapObjStr();
		String key = "";
		for (String a : str) {
			key = "" + Math.abs((Integer.parseInt(a.trim()) - index));
			retmp.put(key, key);
		}
		key = retmp.keySet().toString();
		return addTwoLen(key.substring(1, key.length() - 1));
	}

	/**
	 * 型杀号
	 * @return
	 */
	public String xingSha() {
		String openrednum = openNum.sixRedNum();
		String[] opens = openrednum.split(",");
		int index1 = 0;
		for (String os : opens) {
			if (map_01.get(os) != null) {
				index1++;
			}
		}
		int index2 = 0;
		for (String os : opens) {
			if (map_02.get(os) != null) {
				index2++;
			}
		}

		int index3 = 0;
		for (String os : opens) {
			if (map_03.get(os) != null) {
				index3++;
			}
		}

		int index4 = 0;
		for (String os : opens) {
			if (map_04.get(os) != null) {
				index4++;
			}
		}

		int index5 = 0;
		for (String os : opens) {
			if (map_05.get(os) != null) {
				index5++;
			}
		}

		int index6 = 0;
		for (String os : opens) {
			if (map_06.get(os) != null) {
				index6++;
			}
		}
		Map<String, String> tmp = new HashMap<String, String>();
		String s = "" + index1 + index2 + index3 + index4 + index5 + index6;
		s = s.replaceAll("3", "12").replaceAll("4", "112")
				.replaceAll("5", "122").replaceAll("6", "1122");
		String key = null;
		for (int i = 0, len = s.length(); i < len - 2; i++) {
			key = s.substring(i, i + 2);
			tmp.put(key, key);
		}
		String sha = "";
		String dou = "";
		Map<String, String> retmp = getMapObjStr();
		Set<String> keys = tmp.keySet();
		for (java.util.Iterator<String> it = keys.iterator(); it.hasNext();) {
			sha += dou + mapXing.get(it.next());
			dou = ",";
		}
		String[] ctr = sha.split(",");
		for (String c : ctr)
			retmp.put(c, c);
		sha = retmp.keySet().toString();
		return sha.substring(1, sha.length() - 1).replaceAll(" ", "");

	}

	/**
	 * 利用上期开奖红球号码每两个号码相减的绝对值作为下一期开奖号码杀号的选择
	 * 
	 * @return
	 */
	public String xiangjian() {
		Map<Integer, Integer> map = null;
		map = getMapObj();
		Integer n1key = (Integer.parseInt(openNum.getN_6()) - Integer
				.parseInt(openNum.getN_5()));
		map.put(n1key, n1key);
		Integer n2key = (Integer.parseInt(openNum.getN_5()) - Integer
				.parseInt(openNum.getN_4()));
		map.put(n2key, n2key);
		Integer n3key = (Integer.parseInt(openNum.getN_4()) - Integer
				.parseInt(openNum.getN_3()));
		map.put(n3key, n3key);
		Integer n4key = (Integer.parseInt(openNum.getN_3()) - Integer
				.parseInt(openNum.getN_2()));
		map.put(n4key, n4key);
		Integer n5key = (Integer.parseInt(openNum.getN_2()) - Integer
				.parseInt(openNum.getN_1()));
		map.put(n5key, n5key);
		map.remove(0);
		String s = map.keySet().toString();
		return addTwoLen(s.substring(1, s.length() - 1));
	}

	/**
	 * 利用上期开奖号码减去本期蓝球（不包括"快乐星期天"的蓝球）的绝对值作为下一期的开奖 号码杀号的选择。
	 * 
	 * @return
	 */
	public String JianLan() {
		int blue = 0;
		Map<Integer, Integer> map = null;
		blue = Integer.parseInt(openNum.getN_blue());
		map = getMapObj();
		Integer n1key = Math.abs(Integer.parseInt(openNum.getN_1()) - blue);
		map.put(n1key, n1key);
		Integer n2key = Math.abs(Integer.parseInt(openNum.getN_2()) - blue);
		map.put(n2key, n2key);
		Integer n3key = Math.abs(Integer.parseInt(openNum.getN_3()) - blue);
		map.put(n3key, n3key);
		Integer n4key = Math.abs(Integer.parseInt(openNum.getN_4()) - blue);
		map.put(n4key, n4key);
		Integer n5key = Math.abs(Integer.parseInt(openNum.getN_5()) - blue);
		map.put(n5key, n5key);
		Integer n6key = Math.abs(Integer.parseInt(openNum.getN_6()) - blue);
		map.put(n6key, n6key);
		map.remove(0);
		String s = map.keySet().toString();
		return addTwoLen(s.substring(1, s.length() - 1));
	}

	/**
	 * 利用上二期顺序开奖号码一一对应相减的绝对值作为下一期的开奖号码杀号的选择。
	 * 
	 * @return
	 */
	public String LiangQiJian(OpenNumber o1, OpenNumber o2) {
		Map<Integer, Integer> map = null;
		map = getMapObj();
		Integer n1key = Math.abs(Integer.parseInt(o1.getN_1())
				- Integer.parseInt(o2.getN_1()));
		map.put(n1key, n1key);
		Integer n2key = Math.abs(Integer.parseInt(o1.getN_2())
				- Integer.parseInt(o2.getN_2()));
		map.put(n2key, n2key);
		Integer n3key = Math.abs(Integer.parseInt(o1.getN_3())
				- Integer.parseInt(o2.getN_3()));
		map.put(n3key, n3key);
		Integer n4key = Math.abs(Integer.parseInt(o1.getN_4())
				- Integer.parseInt(o2.getN_4()));
		map.put(n4key, n4key);
		Integer n5key = Math.abs(Integer.parseInt(o1.getN_5())
				- Integer.parseInt(o2.getN_5()));
		map.put(n5key, n5key);
		Integer n6key = Math.abs(Integer.parseInt(o1.getN_6())
				- Integer.parseInt(o2.getN_6()));
		map.put(n6key, n6key);
		map.remove(0);
		String s = map.keySet().toString();
		return addTwoLen(s.substring(1, s.length() - 1));
	}

	/**
	 * 利用上期开奖号码减去本期ＡＣ值的绝对值作为下一期的开奖号码杀号的选择
	 * 
	 * @return
	 */
	public String JianAC() {
		Map<Integer, Integer> map = null;
		map = getMapObj();
		int ac = ToolsUtil.getACValue(openNum.sixRedNum());
		map = getMapObj();
		Integer n1key = Math.abs(Integer.parseInt(openNum.getN_1()) - ac);
		map.put(n1key, n1key);
		Integer n2key = Math.abs(Integer.parseInt(openNum.getN_2()) - ac);
		map.put(n2key, n2key);
		Integer n3key = Math.abs(Integer.parseInt(openNum.getN_3()) - ac);
		map.put(n3key, n3key);
		Integer n4key = Math.abs(Integer.parseInt(openNum.getN_4()) - ac);
		map.put(n4key, n4key);
		Integer n5key = Math.abs(Integer.parseInt(openNum.getN_5()) - ac);
		map.put(n5key, n5key);
		Integer n6key = Math.abs(Integer.parseInt(openNum.getN_6()) - ac);
		map.put(n6key, n6key);
		Integer n1key2 = tabs(Integer.parseInt(openNum.getN_1()) - ac);

		map.put(n1key2, n1key2);
		Integer n2key2 = tabs(Integer.parseInt(openNum.getN_2()) - ac);
		map.put(n2key2, n2key2);
		Integer n3key2 = tabs(Integer.parseInt(openNum.getN_3()) - ac);
		map.put(n3key2, n3key2);
		Integer n4key2 = tabs(Integer.parseInt(openNum.getN_4()) - ac);
		map.put(n4key2, n4key2);
		Integer n5key2 = tabs(Integer.parseInt(openNum.getN_5()) - ac);
		map.put(n5key2, n5key2);
		Integer n6key2 = tabs(Integer.parseInt(openNum.getN_6()) - ac);
		map.put(n6key2, n6key2);
		map.remove(0);
		String s = map.keySet().toString();
		return addTwoLen(s.substring(1, s.length() - 1));
	}

	/**
	 * 利用上期开奖号码减去本期和值的拆分合值作为下一期的开奖号码杀号的选择
	 * 
	 * @return
	 */
	public String HeZHiSplit() {
		Map<Integer, Integer> map = null;
		int ac = 0;
		int n1 = Integer.parseInt(openNum.getN_1());
		int n2 = Integer.parseInt(openNum.getN_2());
		int n3 = Integer.parseInt(openNum.getN_3());
		int n4 = Integer.parseInt(openNum.getN_4());
		int n5 = Integer.parseInt(openNum.getN_5());
		int n6 = Integer.parseInt(openNum.getN_6());
		int total = n1 + n2 + n3 + n4 + n5 + n6;
		if (total > 100) {
			int a = total / 100;
			int b = (total - 100) / 10;
			int c = total - (a * 100 + b * 10);
			ac = a + b + c;
		} else {
			int a = total / 10;
			int c = total - (a * 10);
			ac = a + c;
		}
		map = getMapObj();
		Integer n1key = tabs(n1 - ac);
		Integer n2key = tabs(n2 - ac);
		Integer n3key = tabs(n3 - ac);
		Integer n4key = tabs(n4 - ac);
		Integer n5key = tabs(n5 - ac);
		Integer n6key = tabs(n6 - ac);
		map.put(n1key, n1key);
		map.put(n2key, n2key);
		map.put(n3key, n3key);
		map.put(n4key, n4key);
		map.put(n5key, n5key);
		map.put(n6key, n6key);
		map.remove(0);
		String s = map.keySet().toString();
		return addTwoLen(s.substring(1, s.length() - 1));
	}

	/**
	 * 利用上期开奖号码减去当期尾数合值的拆分合值 作为下一期的开奖号码杀号的选择
	 * 
	 * @return
	 */
	public String weiHe() {
		Map<Integer, Integer> map = null;
		int n1 = Integer.parseInt(openNum.getN_1());
		int n2 = Integer.parseInt(openNum.getN_2());
		int n3 = Integer.parseInt(openNum.getN_3());
		int n4 = Integer.parseInt(openNum.getN_4());
		int n5 = Integer.parseInt(openNum.getN_5());
		int n6 = Integer.parseInt(openNum.getN_6());
		int total = n1 % 10 + n2 % 10 + n3 % 10 + n4 % 10 + n5 % 10 + n6 % 10;
		int a = total / 10;
		int c = total - (a * 10);
		int ac = a + c;
		map = getMapObj();
		Integer n1key = tabs(n1 - ac);
		Integer n2key = tabs(n2 - ac);
		Integer n3key = tabs(n3 - ac);
		Integer n4key = tabs(n4 - ac);
		Integer n5key = tabs(n5 - ac);
		Integer n6key = tabs(n6 - ac);
		map.put(n1key, n1key);
		map.put(n2key, n2key);
		map.put(n3key, n3key);
		map.put(n4key, n4key);
		map.put(n5key, n5key);
		map.put(n6key, n6key);
		map.remove(0);
		String s = map.keySet().toString();
		return addTwoLen(s.substring(1, s.length() - 1));
	}

	private Map<Integer, Integer> getMapObj() {
		return new TreeMap<Integer, Integer>(new Comparator<Integer>() {

			@Override
			public int compare(Integer o1, Integer o2) {
				return o1.compareTo(o2);
			}

		});
	}

	private Map<String, String> getMapObjStr() {
		return new TreeMap<String, String>(new Comparator<String>() {

			@Override
			public int compare(String o1, String o2) {
				return o1.compareTo(o2);
			}

		});
	}

	private int tabs(int i) {
		return i < 0 ? i + 33 : i;
	}

	public void update(OpenNumber op) {
		try {
			DB.getInstance().updateByMethod(op, OpenNumber.class,
					"updateMethod");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private String addTwoLen(String s) {
		s = s.replaceAll(" ", "");
		String[] str = s.split(",");
		List<String> list = new ArrayList<String>();
		String tmp = null;
		for (int i = 0, len = str.length; i < len; i++) {
			tmp = str[i].trim();
			if (Integer.parseInt(tmp) < 10) {
				list.add("0" + tmp);
			} else {
				list.add(tmp);
			}
		}
		s = list.toString();
		return s.substring(1, s.length() - 1).replaceAll(" ", "");

	}

	/**
	 * 
	 * @param currentObj
	 *            当前期
	 * @param nextObj
	 *            下一期
	 * @param prevObj
	 *            上一期
	 */
	public void shahaoToOpenNumber(OpenNumber currentObj, OpenNumber nextObj,
			OpenNumber prevObj) {
		try {
			this.setOpenNum(currentObj);
			// 型杀
			nextObj.setXingsha(this.xingSha());
			nextObj.setXingshatf(ToolsUtil.jisuantf(nextObj,
					nextObj.getXingsha()));
			nextObj.setKaijiangxiangjian(this.xiangjian());
			nextObj.setXinagjiantf(ToolsUtil.jisuantf(nextObj,
					nextObj.getKaijiangxiangjian()));
			nextObj.setKaijiangjianlan(this.JianLan());
			nextObj.setJianglantf(ToolsUtil.jisuantf(nextObj,
					nextObj.getKaijiangjianlan()));
			if (null != prevObj) {
				nextObj.setKaijiangliangqijian(this.LiangQiJian(prevObj,
						currentObj));

				nextObj.setLiangqijiantf(ToolsUtil.jisuantf(nextObj,
						nextObj.getKaijiangliangqijian()));
			}
			nextObj.setKaijiangjianac(this.JianAC());
			nextObj.setJianactf(ToolsUtil.jisuantf(nextObj,
					nextObj.getKaijiangjianac()));
			nextObj.setKaijianghezhisplit(this.HeZHiSplit());
			nextObj.setHezhisplittf(ToolsUtil.jisuantf(nextObj,
					nextObj.getKaijianghezhisplit()));
			nextObj.setKaijiangweihesplit(this.weiHe());
			nextObj.setWeihesplittf(ToolsUtil.jisuantf(nextObj,
					nextObj.getKaijiangweihesplit()));
			nextObj.setMuiltyhezhisplit(this.muiltyHeZhi());
			nextObj.setMuiltyhezhitf(ToolsUtil.jisuantf(nextObj,
					nextObj.getMuiltyhezhisplit()));
			this.update(nextObj);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		try {
			List<IDBBo> list = DB.getInstance().queryBoList(
					"SELECT * FROM OpenNumber order by id asc",
					OpenNumber.class);
			OpenNumber currentObj = null;
			OpenNumber nextObj = null;
			OpenNumber prevObj = null;
			for (int i = 0, len = list.size(); i < len; i++) {
				currentObj = (OpenNumber) list.get(i);
				nextObj = (OpenNumber) list.get(i + 1);
				if (i > 0) {
					prevObj = (OpenNumber) list.get(i - 1);
				}
				KillNumberDb kdb = new KillNumberDb();
				kdb.shahaoToOpenNumber(currentObj, nextObj, prevObj);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
